package com.ruoyi.central.publisher.subscription.operation.station.material;

import com.ruoyi.central.domain.*;
import com.ruoyi.central.publisher.event.OperationStationMaterialEvent;
import com.ruoyi.central.service.ITopCentralMaterialService;
import com.ruoyi.central.service.ITopCentralOperationStationMaterialLogService;
import com.ruoyi.central.service.ITopCentralOperationStationService;
import com.ruoyi.central.service.ITopCentralProductTypeService;
import com.ruoyi.common.exception.base.domian.BaseEntityException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Map;

/**
 * @Classname OpeationStationMaterialEventSubscriptionForLog
 * @Author: sunluquan
 * @Date: 2022/6/29 09:27
 * @Description TODO
 * {@link com.ruoyi.central.domain.TopCentralOperationStationMaterialLog} 订阅 OperationStationMaterialEvent 事件
 */
@Component
public class OperationStationMaterialEventSubscriptionForLog {

    @Autowired
    private ITopCentralOperationStationMaterialLogService operationStationMaterialLogService;
    @Autowired
    private ITopCentralOperationStationService operationStationService;
    @Autowired
    private ITopCentralMaterialService materialService;
    @Autowired
    private ITopCentralProductTypeService materialTypeService;
    /**
     * 订阅 OperationStationMaterialEvent 新增事件
     * 记录当前操作工位 绑定的 物料对象
     *
     * @param event
     */
    @EventListener(value = OperationStationMaterialEvent.class, condition = "#event.getSignEnum().name() == 'INSERT'")
    public void insertEvent(OperationStationMaterialEvent event) {
        TopCentralOperationStationMaterial source = event.getSource();

        String materialId = source.getMaterialId();
        Integer operationStationId = source.getOperationStationId();

        TopCentralOperationStationMaterialLog operationStationMaterialLogOfParams = new TopCentralOperationStationMaterialLog(operationStationId, materialId);


        try {
            operationStationMaterialLogService.selectOne(operationStationMaterialLogOfParams);
        } catch (BaseEntityException e) {
            e.printStackTrace();
            return;
        }
        TopCentralOperationStation operationStation = operationStationService.selectAllMap().get(operationStationId);
        Assert.notNull(operationStation,StringUtils.format("操作工位编号：{}不存在或不可用！",operationStationId));

        TopCentralMaterial material = materialService.selectUsableById(materialId,TopCentralMaterial::getTypeId);
        Assert.notNull(material,StringUtils.format("物料编号：{}不存在或不可用！",materialId));

        TopCentralProductType materialType = materialTypeService.selectAllMap().get(material.getTypeId());
        Assert.notNull(materialType,StringUtils.format("物料编号：{} 绑定的物料类型不存在或不可用！",materialId));

        TopCentralOperationStationMaterialLog operationStationMaterialLogOfInsert = new TopCentralOperationStationMaterialLog(operationStationId, materialId);
        operationStationMaterialLogOfInsert.setMaterialTypeName(materialType.getName());
        operationStationMaterialLogOfInsert.setOperationStationName(operationStation.getName());

        operationStationMaterialLogService.insert(operationStationMaterialLogOfInsert);
    }

}
